package com.utooo.service.supplier;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.constant.ResultCodeConstant;
import com.utooo.dao.supplier.SupplierOrderReconciliationMapper;
import com.utooo.pojo.domain.purchase.order.OrderReconciliationDO;
import com.utooo.pojo.domain.supplier.settlement.SupplierOrderReconciliationDO;
import com.utooo.pojo.dto.common.RestResult;
import com.utooo.service.purchase.OrderReconciliationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class SupplierOrderReconciliationServiceImpl extends ServiceImpl<SupplierOrderReconciliationMapper, SupplierOrderReconciliationDO>
        implements SupplierOrderReconciliationService {

    private final OrderReconciliationService orderReconciliationService;

    @Override
    public RestResult<String> confirmOrderReconciliation(String id) {
        SupplierOrderReconciliationDO reconciliation = this.getById(id);
        if (reconciliation == null) {
            log.error("供货商采购订单业务对账单不存在！{}", id);
            return new RestResult<>(ResultCodeConstant.CODE_400, "供货商采购订单业务对账单不存在！");
        }
        String purchaseOrderReconciliationId = reconciliation.getPurchaseOrderReconciliationId();
        OrderReconciliationDO orderReconciliationDO = orderReconciliationService.getById(purchaseOrderReconciliationId);
        if (orderReconciliationDO == null) {
            log.error("采购订单业务对账单不存在！{}", purchaseOrderReconciliationId);
            return new RestResult<>(ResultCodeConstant.CODE_400, "采购订单业务对账单不存在！");
        }
        orderReconciliationDO.setStatus("3");
        orderReconciliationService.updateById(orderReconciliationDO);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<String> revokeOrderReconciliation(String id) {
        SupplierOrderReconciliationDO reconciliation = this.getById(id);
        if (reconciliation == null) {
            log.error("供货商采购订单业务对账单不存在！{}", id);
            return new RestResult<>(ResultCodeConstant.CODE_400, "供货商采购订单业务对账单不存在！");
        }
        String purchaseOrderReconciliationId = reconciliation.getPurchaseOrderReconciliationId();
        OrderReconciliationDO orderReconciliationDO = orderReconciliationService.getById(purchaseOrderReconciliationId);
        if (orderReconciliationDO == null) {
            log.error("采购订单业务对账单不存在！{}", purchaseOrderReconciliationId);
            return new RestResult<>(ResultCodeConstant.CODE_400, "采购订单业务对账单不存在！");
        }
        this.removeById(id);
        orderReconciliationDO.setStatus("4");
        orderReconciliationService.updateById(orderReconciliationDO);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }
}